const User = require('../model/user.model')
class UserService {
    // 注册
    async createUser(user_account,password) {
        const res = await User.create({
            user_account,
            password
        })
        return res.dataValues
    }

    // 查重
    async getUserInfo({id,user_account,password,user_name}) {
        const whereOpt = {}
        id && Object.assign(whereOpt,{ id })
        user_account && Object.assign(whereOpt,{ user_account })
        password && Object.assign(whereOpt,{ password })
        user_name && Object.assign(whereOpt,{ name })
        const res = await User.findOne({
            attributes: ['id','user_account','password','name','avatar','introduction','roles','createdAt','phone'],
            where: whereOpt
        })
        return  res ? res.dataValues: null
    }

    // 信息修改byid
    async upDataById({id,user_img,user_name,password}) {
        const whereOpt = {id}
        const newUser = {}
        user_img && Object.assign(newUser,{ user_img })
        password && Object.assign(newUser,{ password })
        user_name && Object.assign(newUser,{ user_name })
        const res = await User.update(newUser,{where:whereOpt})
        return res[0] > 0 ? true : false
    }
}

module.exports = new UserService()